package org.eclipse.rally.plugin.views;

import org.apache.log4j.Logger;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.rally.plugin.Activator;
import org.eclipse.rally.plugin.utils.SWTResourceManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;


/**
 * This sample class demonstrates how to plug-in a new
 * workbench view. The view shows data obtained from the
 * model. The sample creates a dummy model on the fly,
 * but a real implementation would connect to the model
 * available either in this or another plug-in (e.g. the workspace).
 * The view is connected to the model using a content provider.
 * <p>
 * The view uses a label provider to define how model
 * objects should be presented in the view. Each
 * view can present the same model objects using
 * different labels and icons, if needed. Alternatively,
 * a single label provider can be shared between views
 * in order to ensure that objects of the same type are
 * presented in the same way everywhere.
 * <p>
 */
public class RallyView extends ViewPart {
    private TableViewer viewer;
    Composite defectcomposite;
    Composite taskcomposite;
    Composite userstorycomposite;
    Composite testcasecomposite;
    Composite composite,composite2;
    Composite compositeHeaderPage;
    LoginWindow loginWindow = null;
    boolean isCancelled = false;
    ScrolledComposite scrolledComposite;
    boolean isNewObject = true;
    
    private Logger logger = Activator.getLogManager().getLogger(RallyView.class.getName());
    /**
     * The constructor.
     */
    public RallyView() {
    }

    /**
     * This is a callback that will allow us
     * to create the viewer and initialize it.
     */
    public void createPartControl(Composite parent) {
       logger.info("****** createPartControl");
       
       try {
            Display.getDefault().syncExec(new Runnable() {
                    public void run() {
                        System.out.println("****** run");
                        loginWindow = new LoginWindow();
                        loginWindow.setBlockOnOpen(true);
                        loginWindow.open();
                    }
                });
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        //Create the initial View Part
        parent.setBackground(SWTResourceManager.getColor(
                SWT.COLOR_LIST_BACKGROUND));
        scrolledComposite = new ScrolledComposite(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
 	   	scrolledComposite.setExpandHorizontal(true);
 	   	scrolledComposite.setExpandVertical(true);
 	   	composite = new Composite(scrolledComposite, SWT.NONE);
 	   	composite.setBounds(0,0, 1200, 600);
 	   	composite.setBackground(SWTResourceManager.getColor(
                SWT.COLOR_LIST_BACKGROUND));
        composite.setLayout(null);
        
        //If ESC or Cancel pressed on Login Window
        if(loginWindow.exitRallyBeforeLogin){
    		System.out.println("DISPOSE RALLY VIEW");
    		Font font = new Font(composite.getDisplay(), "Verdana", 10, SWT.COLOR_RED);
    		String message = "Sorry! you are not connected to Rally. Please <a>close</a> this view and Login to access Rally.";    		
            Link link = new Link(composite, SWT.NONE);
            link.setFont(font);
            link.setText(message);
            link.setBackground(SWTResourceManager.getColor(
                    SWT.COLOR_LIST_BACKGROUND));
            link.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
            //logout.setFont(font);
            link.setBounds(2,2, 650, 20);
            link.addListener(SWT.Selection,
                new Listener() {
                    public void handleEvent(Event event) {
                    	new HeaderPage(RallyView.this).closeRallyView();
                    }  	
                });
    	}else{//Else if Login clicked
	        HeaderPage header = new HeaderPage(this);
	        header.createContents(composite);
	        TabFolder tabFolder = new TabFolder(composite, SWT.NONE);
	        tabFolder.setBounds(0,36, 938, 600);
	        
	        new DashboardGUI().dashBoard(tabFolder);
	        defectcomposite = new Composite(tabFolder, SWT.NONE);
	        
	        new DefectsTabGUI().DefectsTabData(tabFolder);
	        userstorycomposite = new Composite(tabFolder, SWT.NONE);
	        new UserStoryTabGUI().UserStoryTabData(tabFolder,true);
	        
	        taskcomposite = new Composite(tabFolder, SWT.NONE);
	        new TaskTabGUI().TaskTabData(tabFolder,isNewObject);
	
	        testcasecomposite = new Composite(tabFolder, SWT.NONE);
	        new TestCaseTabGUI().TestCaseTabData(tabFolder);
		       
 	         header.setTabFolder(tabFolder);
 	         
		   
	        scrolledComposite.setContent(composite);
	     	scrolledComposite.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
	        viewer = new TableViewer(tabFolder,SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
	        
    	}
        viewer = new TableViewer(composite,SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
        viewer.setContentProvider(new ViewContentProvider());
        viewer.setLabelProvider(new ViewLabelProvider());
        viewer.setSorter(new NameSorter());
        viewer.setInput(getViewSite());
        } 

    /**
     * Passing the focus request to the viewer's control.
     */
    public void setFocus() {
		System.out.println("SET FOCUS");
		viewer.getControl().setFocus();
    }

    /*
     * The content provider class is responsible for
     * providing objects to the view. It can wrap
     * existing objects in adapters or simply return
     * objects as-is. These objects may be sensitive
     * to the current input of the view, or ignore
     * it and always show the same content
     * (like Task List, for example).
     */
    class ViewContentProvider implements IStructuredContentProvider {
        public void inputChanged(Viewer v, Object oldInput, Object newInput) {
        }

        public void dispose() {
        }

        public Object[] getElements(Object parent) {
            return new String[] { "One", "Two", "Three" };
        }
    }

    class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {
        public String getColumnText(Object obj, int index) {
            return getText(obj);
        }

        public Image getColumnImage(Object obj, int index) {
            return getImage(obj);
        }

        public Image getImage(Object obj) {
            return PlatformUI.getWorkbench().getSharedImages()
                             .getImage(ISharedImages.IMG_OBJ_ELEMENT);
        }
    }

    class NameSorter extends ViewerSorter {
    }
}
